package com.accp.service.impl;

import com.accp.entity.Hotevents;
import com.accp.entity.Users;
import com.accp.mapper.HoteventsMapper;
import com.accp.mapper.UsersMapper;
import com.accp.service.CommentsService;
import com.accp.service.HoteventsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jack
 * @since 2021-08-03
 */
@Service
public class HoteventsServiceImpl implements HoteventsService {
	
	@Autowired
	HoteventsMapper eventMapper;
	
	@Autowired
	CommentsService commentService;
	
	@Autowired
	UsersMapper userMapper;

	@Override
	public IPage<Hotevents> getEventByPage(int pageIndex, int pageRows, String keyword) {
		Page pageflag = new Page(pageIndex, pageRows);
		QueryWrapper<Hotevents> qu = new QueryWrapper<Hotevents>();
		if(keyword!=null && keyword.length()>0){
			//加入筛选条件
			qu.like("keyWord", keyword);
		}
		qu.orderByDesc("searchSum");
		IPage pageData = this.eventMapper.selectPage(pageflag, qu);
		return pageData;
	}

	@Override
	public Hotevents fetchEvent(int id) {
		Hotevents event = this.eventMapper.selectById(id);
		if(event == null){
			//return null;
			throw new RuntimeException("该热点事件不存在");
		}
		//查询uid所对应的用户
		Users user = this.userMapper.selectById(event.getUid());
		//清除密码
		user.setPassword(null);
		event.setUser(user);
		//查出该热点事件的评论列表
		event.setComments(this.commentService.getCommentsByHid(event.getId()));
		return event;
	}

	@Override
	public int addEvent(Hotevents event) {
		event.setCreateDate(new java.sql.Date(System.currentTimeMillis()));
		event.setSearchSum(0);
		return this.eventMapper.insert(event);
	}

	@Override
	public int updateEvent(Hotevents event) {
		return this.eventMapper.updateById(event);
	}

}
